/**
 * @author QinGuan
 * @date:2016-1-8 下午01:10:41
 * @version :3.0
 *
 */
package com.ctvit.wsservice.basedata.service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.ctvit.framework.dao.Condition;
import com.ctvit.framework.service.BaseService;
import com.ctvit.wsservice.basedata.domain.ZcdmTChange;
import com.ctvit.wsservice.basedata.domain.ZcdmTChangesub;
import com.ctvit.wsservice.basedata.domain.ZcdmTProglist;
import com.ctvit.wsservice.basedata.domain.ZcdmTProrec;
import com.ctvit.wsservice.basedata.domain.ZppmTProginfo;
import com.ctvit.wsservice.basedata.domain.ZppmTask;

@SuppressWarnings("serial")
public class TaskChangePortService extends BaseService {
	/**
	 * 
	 * @ps:节目变更单主表和子表的保存
	 * @ps:先判断在zcdm_t_change表中是否存在,存在则不插入,不存在则更新并继续,
	 * @ps:再循环判断集合zcdmTChangesubs的对象在节目单子表 zcdm_t_proglist 中是否存在,存在则继续,不存在则不操作,
	 * @ps:保存 zcdmTChange对象和zcdmTChangesubs中的zcdmTChangesub对象,
	 * @param zcdmTChange
	 * @param zcdmTChangesubs
	 * @return -1 参数无效 0 插入成功 1数据已存在,执行更新操作上
	 */
	public int save(ZcdmTChange zcdmTChange,
			List<ZcdmTChangesub> zcdmTChangesubs) {
		if (zcdmTChange == null && zcdmTChangesubs == null)
			return -1;
		genericDao.insert(zcdmTChange);
		genericDao.batchInsert(zcdmTChangesubs);
		return 0;// 操作成功
	}

	/**
	 * 
	 * @ps:修改锁定状态时保存节目单主表
	 * @param zcdmTProrec
	 * @return
	 */
	public int saveZcdmTProrec(ZcdmTProrec zcdmTProrec) {
		if (zcdmTProrec == null) {
			return -1;
		}
		zcdmTProrec.setZUptim(new Timestamp(Calendar.getInstance().getTimeInMillis()));
		Condition condition = new Condition();
		condition.add("zProRecCd", zcdmTProrec.getZProRecCd());
		if (genericDao.updateByCondition(zcdmTProrec,
				new String[] { "zLockstatus","zUptim" }, condition) >= 0) {
			return 0;
		}
		return 1;
	}

	/**
	 * 
	 * @ps:变更单子表的变更后内容,保存到节目单子表中
	 * @param zcdmTProglists 节目单子表的集合
	 * @return int
	 */
	public int saveChangesubToProglist(List<ZcdmTProglist> zcdmTProglists,List<ZcdmTChangesub> ListzcdmTChangesubBefore) {
		if (zcdmTProglists == null)
			return -1;
		for (int i = 0; i < ListzcdmTChangesubBefore.size(); i++) {
			int j = deleteZcdmTProglist(ListzcdmTChangesubBefore.get(i).getZProItemCd());// 根据变更前子表删除节目单子表内容
			if (j <= 0) {
				// 抛出异常!
//				throw new RuntimeException("变更前子表删除操作失败!");
			} 
		}
		int num = 0;// 记录保存的数据条数
		Condition condition = null;
		ZcdmTProglist zcdmTProglist = null;
		for (int i = 0; i < zcdmTProglists.size(); i++) {
			zcdmTProglist = zcdmTProglists.get(i);
			condition = new Condition();
			condition.add("zProItemCd", zcdmTProglist.getZProItemCd());
			if (genericDao.updateByCondition(zcdmTProglist,
					new String[] { "zProItemCd" }, condition) == 0) {
				genericDao.insert(zcdmTProglist);
				num++;
			}
		}
		return num;
	}

	/**
	 * 
	 * @ps: 判断变更单主表是否存在,不存在返回null
	 * @param zcdmTChange
	 * @return
	 */
	public ZcdmTChange isZcdmTChangeExist(ZcdmTChange zcdmTChange) {
		if (zcdmTChange == null)
			return null;
		Condition condition = new Condition();
		condition.add("zChanCd", zcdmTChange.getZChanCd());
		if (genericDao.getObjectByCondition(zcdmTChange.getClass(), condition) == null) {
			return null;
		}
		return zcdmTChange;// 存在
	}

	/**
	 * 
	 * @ps:节目变更前的内容在节目单的子表是否存在
	 * @param broadcastListItemID 播出单条目标识
	 * @param date 播出日期
	 * @param chanlCode 播出频道代码
	 * @return boolean
	 */
	public boolean isZcdmTProglistExist(String broadcastListItemID, Date date,
			String chanlCode) {
		if (broadcastListItemID == null)
			return false;
		SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
		Condition condition = new Condition();
		ZcdmTProglist zcdmTProglist = new ZcdmTProglist();
		zcdmTProglist.setZProItemCd(broadcastListItemID);
		zcdmTProglist.setZPlayDate(new Timestamp(date.getTime()));
		zcdmTProglist.setZChanlCd(chanlCode);
		condition.add("zProItemCd", zcdmTProglist.getZProItemCd());// 播出单条目标识
		condition.add("zPlayDate", "and",format.format(zcdmTProglist.getZPlayDate()));// 播出日期
		condition.add("zChanlCd", zcdmTProglist.getZChanlCd());// 播出频道代码
		if (genericDao
				.getObjectByCondition(zcdmTProglist.getClass(), condition) == null) {
			return false;// 不存在
		}
		return true;// 存在
	}

	/**
	 * 
	 * @ps:根据播出单条目标识,删除对应的数据
	 * @param broadcastListItemID 播出单条目标识
	 * @return/boolean true:删除成功 false:删除失败
	 */
	public int deleteZcdmTProglist(String broadcastListItemID) {
		if (broadcastListItemID == null)
			return 0;
//		String sql="update qrtz_sync_timestamp set PKVALUE=concat(PKVALUE,',"+broadcastListItemID+"') where SOURCESYS='LOCAL' and DESTSYS='REMOTE' and NAME='ZCDM_T_PROGLIST'";
//		genericDao.updateBySqlStr(sql);
		ZcdmTProglist zcdmTProglist = new ZcdmTProglist();
		Condition condition = new Condition();
		zcdmTProglist.setZProItemCd(broadcastListItemID);
		condition.add("zProItemCd", zcdmTProglist.getZProItemCd());
		return genericDao
				.removeByCondition(zcdmTProglist.getClass(), condition);// 删除的数目
	}

	/**
	 * 
	 * @ps:更新任务信息
	 * @param zppmTask
	 * @param zppmTProginfo
	 * @return
	 */
	public String updateZppmTaskProInfo(ZppmTask zppmTask,
			ZppmTProginfo zppmTProginfo) {
		String _return = null;// 返回信息
		if (zppmTask == null && zppmTProginfo == null) {
			_return = "参数异常!";
			return _return;
		}
		Condition condition = new Condition();
		condition.add("zTaskId", zppmTask.getZTaskId());
		int i = genericDao.updateByCondition(zppmTask, new String[] {
				"zTaskName", "zAssignTime","zConrecDone","zStockDone","zTechDone" }, condition);
		if (i>0){
			_return = "任务信息更新成功,";
		}else{
			genericDao.insert(zppmTask);
			_return = "任务信息插入成功,";
		}
		condition = new Condition();
		condition.add("zProcode", zppmTProginfo.getZProcode());
		condition.add("zAudVer", zppmTProginfo.getZAudVer());
		condition.add("zCapVer", zppmTProginfo.getZCapVer());
		int j = genericDao.updateByCondition(zppmTProginfo, null, condition);
		if (j > 0) { 
			_return += "节目信息更新成功";
		}else{
			_return += "节目信息插入成功";
			genericDao.insert(zppmTProginfo);
		}
		return _return;
	}
}
